﻿using ConfExpert.Forms.Base;
using DevExpress.XtraEditors.Controls;
using DevExpress.XtraEditors.Repository;
using DevExpress.XtraGrid.Columns;
using DevExpress.XtraGrid.Views.Grid;
using DevTeam;
using System;
using System.Data;
namespace ConfExpert.Utility
{
    public partial class ExcelView : XForm
    {
        public ExcelView()
        {
            InitializeComponent();
        }

        private void ExcelView_Load(object sender, EventArgs e)
        {
            btnRun.ItemClick += btnRun_ItemClick;
        }

        void btnRun_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            gridView1.EndEditing();
            DataTable dtx = ExecuteDataTableByQuery(txtQuery.EditValue.ToStr());
            gridView1.Init(dtx);

            /*
            AddScan(gridView1, "S1", "S2", "S3", "S4", "S5", "S6");
            gridView1.SetCaptions("S1:Sc1", "S2:Sc2", "S3:Sc3", "S4:Sc4", "S5:Sc5", "S6:Sc6", "Total:Gömlek Adedi");
            gridView1.YanYanaGetir_Soluna("ToplamG6", 
                "S1", "Product1Id", "ToplamG1",
                "S2", "Product2Id", "ToplamG2",
                "S3", "Product3Id", "ToplamG3",
                "S4", "Product4Id", "ToplamG4",
                "S5", "Product5Id", "ToplamG5",
                "S6", "Product6Id");

            gridView1.FormatAsNumber(2, "ToplamG1", "ToplamG2", "ToplamG3", "ToplamG4", "ToplamG5", "ToplamG6");
            gridView1.CustomUnboundColumnData -= gridView1_CustomUnboundColumnData;
            gridView1.CalcRowHeight -= gridview_CalcRowHeight;

            gridView1.CustomUnboundColumnData += gridView1_CustomUnboundColumnData;
            gridView1.CalcRowHeight += gridview_CalcRowHeight;
            */

            gridView1.MakeReadOnly();
            gridView1.AddSummary();
            gridView1.BestFitColumns();

        }

        public void gridView1_CustomUnboundColumnData(object sender, DevExpress.XtraGrid.Views.Base.CustomColumnDataEventArgs e)
        {
            if (e.ListSourceRowIndex > -1 &&
                e.Column.FieldName.StartsWith("S") && e.Column.FieldName.Length == 2)
                if (e.IsGetData)
                {
                    string col = e.Column.FieldName.Replace("S", "Product") + "Id";

                    if (((DataRowView)e.Row)[col].ToInt() > 0)
                    {
                        DataRow[] dr = AppSession.Lookups.V_Kumas.Select("ProductId=" + ((DataRowView)e.Row)[col].ToString());
                        if (dr.Length > 0)
                            if (dr[0]["PhotoMikro"] != DBNull.Value)
                                e.Value = dr[0]["PhotoMikro"];
                    }
                }
        }

        void AddScan(GridView gridview, params string[] FieldNames)
        {
            foreach (string FieldName in FieldNames)
            {
                GridColumn colPhoto = new GridColumn();
                colPhoto.FieldName = FieldName;
                gridview.Columns.Add(colPhoto);

                colPhoto.ColumnEdit = new RepositoryItemPictureEdit();
                colPhoto.UnboundType = DevExpress.Data.UnboundColumnType.Object;

                (colPhoto.ColumnEdit as RepositoryItemPictureEdit).SizeMode = PictureSizeMode.Clip;
                (colPhoto.ColumnEdit as RepositoryItemPictureEdit).NullText = " ";
            }
        }

    }

}

